package com.adwin.addcoin.ui.dialog

import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.adwin.addcoin.base.BaseDialog
import com.adwin.addcoin.databinding.DialogClearCacheBinding
import com.adwin.addcoin.utils.CacheDataManager
import com.adwin.addcoin.utils.installRepost
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.json.JSONObject

class ClearCacheDialog(val mContext: Context): BaseDialog(mContext) {
    lateinit var binding: DialogClearCacheBinding

    override fun getGravity(): Int = Gravity.CENTER

    override fun getWidth(): Int  = ViewGroup.LayoutParams.MATCH_PARENT

    override fun getHeight(): Int = ViewGroup.LayoutParams.MATCH_PARENT

    override fun initLayout(): View {
        binding = DialogClearCacheBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initData() {
        GlobalScope.launch(Dispatchers.IO) {
            context.installRepost("ClearCacheDialogShow")
        }
        binding.tvCacheNo.setOnClickListener {
            GlobalScope.launch(Dispatchers.IO) {
                context.installRepost("ClearCacheNoClick")
            }
            dismiss()
        }
        binding.tvCacheYes.setOnClickListener {
            MainScope().launch(Dispatchers.IO) {
                context.installRepost("ClearCacheOkClick")
                CacheDataManager.clearAllCache(context)
                withContext(Dispatchers.Main){
                    dismiss()
                }
            }
        }
    }
}